//
//  DataWebViewModel.m
//  SHRCWDemo
//
//  Created by 01 on 17/2/28.
//  Copyright © 2017年 蔡正军. All rights reserved.
//

#import "DataWebViewModel.h"
#import "E_ScrollViewController.h"

@interface DataWebViewModel ()

@property (nonatomic, strong) NSString *title;

@property (nonatomic, strong) NSString *text;

@end

@implementation DataWebViewModel

-(void)dealloc
{
    self.dataWebView.delegate = nil;
    [self.dataWebView stopLoading];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        _currentDataStatus = currentDataStauts;
    }
    return self;
}

- (void)setDataWebUrl:(NSString *)dataWebUrl
{
    _dataWebUrl = dataWebUrl;
    NSLog(@"==== url = %@", _dataWebUrl);
}

- (void)setLeftDataStr:(NSString *)leftDataStr
{
    _leftDataStr = leftDataStr;
}

- (void)setCurrentDataStr:(NSString *)currentDataStr
{
    _currentDataStr = currentDataStr;
}

- (void)setRightDataStr:(NSString *)rightDataStr
{
    _rightDataStr = rightDataStr;
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSString *titleStr = [NSString stringWithFormat:@"function textTiele()"
                         "{"
                         "return document.getElementById(\"%@\").innerText;"
                         "}", [JSTSystemInfo readerTitle]];
    [webView stringByEvaluatingJavaScriptFromString:titleStr];
    _title = [webView stringByEvaluatingJavaScriptFromString:@"textTiele()"];
    
    NSString *textStr = [NSString stringWithFormat:@"function textContent()"
                         "{"
                         "return document.getElementById(\"%@\").innerText;"
                         "}", [JSTSystemInfo readerText]];
    [webView stringByEvaluatingJavaScriptFromString:textStr];
    _text = [webView stringByEvaluatingJavaScriptFromString:@"textContent()"];
    
    [self currentWebView:webView];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"----- dataWeb error = \n%@", error);
    [[NSNotificationCenter defaultCenter] postNotificationName:@"readReloadError" object:nil];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    _dataWebUrl = request.URL.absoluteString;
    return YES;
}

- (void) currentWebView:(UIWebView *)webView
{
    switch (_currentDataStatus)
    {
            case leftDataStatus:
        {
            _isLeftSuccess = YES;
            DLog(@"------------进入左页");
            if (kPublicModel.currentUrl && kPublicModel.rightText) {
                kPublicModel.rightUrl = _otherUrl;
                if (IsStrEmpty(_text)) {
                    kPublicReadCtl.dataStatus = leftStatus;
                    kPublicReadCtl.data = [NSMutableArray arrayWithObjects: _currentDataStr, _rightDataStr, nil];
                }else{
                    _leftDataStr = [NSString stringWithFormat:@"%@\n%@", _title, _text];
                    kPublicReadCtl.data = [NSMutableArray arrayWithObjects:_leftDataStr, _currentDataStr, _rightDataStr, nil];
                }
            }else{
                if (IsStrEmpty(_text)) {
                    _leftDataStr = nil;
                }else{
                    kPublicModel.leftUrl = _dataWebUrl;
                    _leftDataStr = [NSString stringWithFormat:@"%@\n%@", _title, _text];
                }
                _currentDataStatus = currentDataStauts;
                if ([kPublicModel.currentUrl isEqualToString:kPublicModel.leftUrl]) {
                    _leftDataStr = nil;
                    kPublicModel.leftUrl = nil;
                    _currentDataStatus = rightDataStatus;
                    NSString *nextStr = [NSString stringWithFormat:@"document.getElementById(\"%@\").click();", [JSTSystemInfo readerNext]];
                    [webView stringByEvaluatingJavaScriptFromString:nextStr];
                }else [webView goBack];
            }
        }
            break;
            case currentDataStauts:
        {
            DLog(@"------------进入当前页");
            if (!IsStrEmpty(_leftDataStr) || _isLeftSuccess) {
                _currentDataStatus = rightDataStatus;
                NSString *nextStr = [NSString stringWithFormat:@"document.getElementById(\"%@\").click();", [JSTSystemInfo readerNext]];
                [webView stringByEvaluatingJavaScriptFromString:nextStr];
            }else {
                kPublicModel.currentUrl = _dataWebUrl;
                _currentDataStr = [NSString stringWithFormat:@"%@\n%@", _title, _text];
                _currentDataStatus = leftDataStatus;
                NSString *preStr = [NSString stringWithFormat:@"document.getElementById(\"%@\").click();", [JSTSystemInfo readerPrevious]];
                [webView stringByEvaluatingJavaScriptFromString:preStr];
            }
        }
            break;
            case rightDataStatus:  
        {
            DLog(@"------------进入右页");
            if (IsStrEmpty(_text)) {
                kPublicReadCtl.dataStatus = rightStatus;
                if (IsStrEmpty(_leftDataStr)) {
                    kPublicReadCtl.data = [NSMutableArray arrayWithObjects:_currentDataStr, nil];
                }else kPublicReadCtl.data = [NSMutableArray arrayWithObjects: _leftDataStr, _currentDataStr, nil];
            }else{
                kPublicModel.rightUrl = _dataWebUrl;
                _rightDataStr = [NSString stringWithFormat:@"%@\n%@", _title, _text];
                if (IsStrEmpty(_leftDataStr)) {
                    kPublicReadCtl.data = [NSMutableArray arrayWithObjects:_currentDataStr, _rightDataStr, nil];
                }else
                kPublicReadCtl.data = [NSMutableArray arrayWithObjects:_leftDataStr, _currentDataStr, _rightDataStr, nil];
            }
        }
            break;
        default:
            break;
    }
}

- (UIWebView *)dataWebView
{
    if (!_dataWebView) {
        _dataWebView = [[UIWebView alloc] initWithFrame:CGRectZero];
        _dataWebView.delegate = self;
    }
    return _dataWebView;
}

@end
